#include "nclient.h"
#include "ui_nclient.h"

nclient::nclient(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::nclient)
{
    ui->setupUi(this);
    connect(ui->cancel, &QPushButton::clicked, this, &nclient::cc);
    connect(ui->YesButton, &QPushButton::clicked, this, &nclient::save);
}

nclient::~nclient()
{
    delete ui;
}

void nclient::cc() {
    this->close();
}

void nclient::save() {
    QString ClientID = ui->ClientID->text();
    QString Cname = ui->Cname->text();
    QString CCode = ui->CCode->text();
    QString CAdd = ui->CAdd->text();
    QString CLevel = ui->CLevel->text();
    QString CMoney = ui->CMoney->text();


    // 验证输入不为空
    if (ClientID.isEmpty() || Cname.isEmpty() || CCode.isEmpty() || CAdd.isEmpty() || CLevel.isEmpty() || CMoney.isEmpty()) {
        QMessageBox::warning(this, "错误", "请填写所有必要信息！");
        return;
    }

    // 执行SQL插入
    QSqlDatabase db = QSqlDatabase::database("qt_sql_default_connection");
    if (!db.isOpen()) {
        QMessageBox::warning(this, "错误", "数据库未连接！");
        return;
    }

    QSqlQuery query(db);
    query.prepare("INSERT INTO ClientList (ClientID, Cname, CCode, CAdd, CLevel, CMoney) "
                  "VALUES (?, ?, ?, ?, ?, ?)");
    query.addBindValue(ClientID);
    query.addBindValue(Cname);
    query.addBindValue(CCode);
    query.addBindValue(CAdd);
    query.addBindValue(CLevel);
    query.addBindValue(CMoney);

    if (query.exec()) {
        QMessageBox::information(this, "成功", "数据添加成功！");
        this->close();  // 关闭窗口
    } else {
        QMessageBox::critical(this, "错误",
                              QString("数据添加失败"));
    }
}
